<template>
  <div class="view-container bg-gray">
    <div class="detail-container">
      <div class="header">
        <p class="title-1" v-text="notice.noticeTitle"></p>
        <p class="sub-title">
          <span>发布于：{{notice.created}}</span>
          <span class="separate">|</span>
          <span v-text="notice.noticeType"></span>
        </p>
      </div>
      <div class="section" v-html="notice.noticeContent"></div>
      <div class="footer">
        <div class="item">
          <svg-icon icon-class="print1"></svg-icon>
          <span>打印</span>
        </div>

        <div class="item" @click="addToFav">
          <svg-icon icon-class="icon_star"></svg-icon>
          <span>收藏</span>
        </div>
        <div class="item">
          <svg-icon icon-class="icon_message"></svg-icon>
          <span>回复<span class="comment-number">({{commentList.length}})</span></span>
        </div>
        <!--<span><a href="javascript:;">打印</a></span>
        <span><a href="javascript:;" @click="addToFav">收藏</a></span>
        <span>评论</span>
        <span class="comment-number">({{commentList.length}})</span>-->
      </div>
      <div class="comment-container">
        <div class="comment-input-wrap">
          <el-input type="textarea" :rows="4" placeholder="我来说两句" v-model="commentsContent"></el-input>
          <div class="do-comment">
            <el-button-group>
              <el-button>@</el-button>
              <el-button type="primary" @click="addComment">评论</el-button>
            </el-button-group>

          </div>
        </div>
        <div class="comment-list" v-if="commentList.length !== 0">
          <transition-group tag="ul" name="list">
            <li v-for="item in commentList" :key="item.cmtId" @mouseover="item.opacity = 1" @mouseout="item.opacity = 0">
              <div class="block-1" >
                <div class="user-info">
                  <div class="img-wrap">
                    <img :src="item.photo" alt="">
                  </div>
                  <div class="info-detail">
                    <p v-text="item.creator"></p>
                    <p v-text="item.timeDiff"></p>
                  </div>
                </div>
                <div class="sdf">
                  <div>
                    <a href="javascript:;">回复</a>
                  </div>
                  <div :style="{opacity:item.opacity}" class="del-div">
                    <a href="javascript:;" @click=delFel(item.cmtId)>删除</a>
                  </div>
                </div>
              </div>

              <div class="comment-content" v-text="item.cmtConent"></div>
            </li>
          </transition-group>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
  import noticeAPI from '@/api/portal/notice'
  import commentsAPI from '@/api/comments/comments'
  import favAPI from '@/api/shouzang/SeleAll'

  export default {
    name: 'NoticeDetail',
    data() {
      return {
        notice: {
          noticeId: this.$route.params.id
        },
        commentList: [],
        commentsContent: '',
        timer: null,
        favLoading: false
      }
    },
    computed: {
      isCanAdd() {
        return this.commentsContent.trim() === ''
      }
    },
    created() {
      if(!this.notice.noticeId){
        let url = this.location.href
        this.notice.noticeId =  url.substring(url.lastIndexOf('/')+1)
      }
      this.fetchData()
      this.fetchComments()
    },
    methods: {
      fetchData() {
        noticeAPI.getById(this.notice.noticeId).then(res => {
          console.log('noticeAPI.getById',res.result)
          this.notice = res.result
          this.notice.created = new Date(this.notice.created).Format('yyyy-MM-dd hh:mm:ss')
          this.notice.noticeType = parseInt(this.notice.noticeType) === 1 ? '通知' : '公告'
        })
      },
      fetchComments() {
        commentsAPI.list(this.notice.noticeId).then(res => {
          const dataArr = res.result
          for (const val of dataArr) {
            val.isShowDel = false
            val.opacity = 0
          }
          this.commentList = dataArr
        })
      },

      addComment() {
        if(this.commentsContent.trim() === ''){
          this.$message.info('请输入评论')
          return
        }
        commentsAPI.save({
          refId: this.notice.noticeId,
          cmtConent: this.commentsContent.trim()
        }).then(res => {
          this.fetchComments()
        })
        this.commentsContent = ''
      },
      showDelBtn(item) {
        item.isShowDel = true
      },
      hiddenDelBtn(item) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          item.isShowDel = false
        }, 200)
      },
      addToFav() {
        if (this.favLoading) {
          return
        }
        this.favLoading = true
        favAPI.addshouz({
          favTitle: this.notice.noticeTitle,
          favType: '测试',
          favUrl: location.href.replace(/^http:\/\/([0-9]+.){3}[0-9]+:[0-9]+/,'')
        }).then(res => {
          this.favLoading = false
          this.$message.success('收藏成功')
        })
      },
      delFel(id){
        noticeAPI.deletest(id).then(res=>{
          this.fetchComments()
        })
      },
    }
  }
</script>

<style lang="scss">
  .detail-container {
    .section {
      a {
        color: #1890ff;

        &:hover {
          text-decoration: underline;
        }
      }
    }

  }
</style>

<style scoped lang="scss">

  /*评论列表过渡*/
  .list-enter-active, .list-leave-active {
    transition: all .5s;
  }
  .list-enter, .list-leave-to
    /* .list-leave-active for below version 2.1.8 */ {
    opacity: 0;
    transform: translateX(-30px);
  }

  .detail-container {
    margin: 0 50px;
    padding: 0 50px 50px;
    background-color: white;

    a:hover {
      text-decoration: underline;
    }
  }

  .header {
    text-align: center;
    border-bottom: 1px solid #ccc;
    padding-bottom: 10px;
    padding-top: 1px;

    .title-1 {
      line-height: 30px;
      margin: 20px 0;
    }

    .separate {
      margin: 0 5px;
    }

    .sub-title {
      font-size: 12px;
      color: gray;
    }
  }

  .header, .section {
    border-bottom: 1px solid #ccc;
  }

  .section {
    padding: 20px 0;
    line-height: 40px;

    a {
      color: #1890ff;
    }
  }

  .footer {
    padding: 10px;
    font-size: 14px;
    line-height: 15px;
    margin: 10px 0;
    display: flex;
    flex-direction: row-reverse;
    transition: color 0.5s;
    cursor: pointer;

    .item {
      margin-right: 20px;
      /*&:last-of-type {
        margin-right: 5px;
      }*/
      &:hover {
        color: #919191;
        text-decoration: underline;
      }
    }

    .comment-number {
      margin-left: 5px;
      font-size: 16px;
      color: orangered;
    }
  }

  .comment-container {
    .comment-input-wrap {

    }
    .do-comment {
      text-align: right;
      margin: 10px 0 20px 0;
      .tip {
        font-size: 12px;
        color:gray;
        margin-right: 10px;
      }
    }
  }

  .comment-list {
    border: 1px solid #ccc;
    border-radius: 3px;
    padding: 0 10px;

    li {
      /*display: flex;*/
      padding: 10px;
      border-bottom: 1px solid #ccc;

      &:last-of-type {
        border-bottom: none;
      }
    }

    .block-1 {
      display: flex;
      justify-content: space-between;
      .user-info {
        float: left;
        display: flex;
        .img-wrap {
          border: 1px solid #eee;
          box-shadow: 0 0 1px rgba(0,0,0,0.1);
          margin-right: 10px;
          $photoSize:50px;
          width: $photoSize;
          height: $photoSize;
          img {
            width: 100%;
            height: 100%;
            object-fit: contain;
          }
        }
        .info-detail {
          padding: 5px 0;
          margin-right: 10px;
          font-size: 13px;
          color:gray;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
        }
        .del {
          padding-top: 5px;
          font-size: 13px;
          color:gray;
        }
/*        .el-button {
          float: right;
        }*/
/*        .el-link,.el-button {
          height: initial;
          padding: initial;
        }*/
      }

      .sdf {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        /*text-align: right;*/
        text-align: center;
/*        div:first-of-type {
          margin-bottom: 5px;
        }*/
        .del-div {
          transition: opacity 0.1s;
        }
        a {
          font-size: 13px;
          color:gray;
          text-decoration: none;
          transition: color 0.3s ease-in-out;
          &:hover {
            color: #bbbbbb;
          }
        }
      }
    }

    .comment-content {
      padding: 10px 0;
      font-size: 13px;
      line-height: 25px;
    }

/*    .content-wrap {
      flex-grow: 1;

      .content {
        padding: 5px;
        line-height: 25px;
        font-size: 14px;
      }

      .info {
        padding: 5px;
        font-size: 12px;
        color: #423009;

        .separate {
          margin: 0 5px;
        }

        .pub-user {
          font-size: 14px;
          color: #6c6351;
          font-weight: bold;
        }
      }
    }*/
  }
</style>
